import './tree_node_data.dart';
import 'package:flutter/material.dart';

class TreeNode extends StatefulWidget {
  final TreeNodeData data;

  final double offsetLeft;

  final Widget title;
  final Widget leading;
  final Widget trailing;

  final Function titleOnTap;
  final Function leadingOnTap;
  final Function trailingOnTap;

  const TreeNode(this.data,{
    this.title,
    this.leading = const IconButton(
      icon: Icon(Icons.star_border),
      iconSize: 16,
      onPressed: null,
    ),
    this.trailing = const IconButton(
      icon: Icon(Icons.expand_more),
      iconSize: 16,
      onPressed: null,
    ),
    this.offsetLeft = 24.0,
    this.titleOnTap,
    this.leadingOnTap,
    this.trailingOnTap,
  });

  @override
  _TreeNodeState createState() => _TreeNodeState();

}

class _TreeNodeState extends State<TreeNode>
    with SingleTickerProviderStateMixin {

  AnimationController _rotationController;
  final Tween<double> _turnsTween = Tween<double>(begin: 0.0, end: -0.5);

  initState() {
    _rotationController = AnimationController(
      duration: const Duration(milliseconds: 300),
      vsync: this,
    );

    if (widget.data.expaned) {
      _rotationController.forward();
    } else {
      _rotationController.reverse();
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    TreeNodeData nodeData = widget.data;
    final level = widget.data.level;
    final children = widget.data.children;
    final offsetLeft = widget.offsetLeft;

    Color currentColor = widget.data.isSelect != null && widget.data.isSelect ?Colors.blueAccent : Colors.black38;

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Padding(
          padding: const EdgeInsets.only(left: 12.0),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              GestureDetector(
                onTap: () {
                  if (widget.leadingOnTap != null &&
                      widget.leadingOnTap is Function) {
                    widget.leadingOnTap(widget.data);
                  }
                },
                child: Center(
                  child:
                      IconButton(
                        icon:  Icon(Icons.star_border,color: currentColor,),
                        iconSize: 16,
                        onPressed: null,
                      ),
                ),
              ),
              SizedBox(width: 6.0),
              Expanded(
                child: GestureDetector(
                  onTap: () {
                    if (widget.titleOnTap != null &&
                        widget.titleOnTap is Function) {
                      widget.titleOnTap(widget.data);
                    }
                  },
                  child: widget.title ?? Container(child: Text(nodeData.title,style: TextStyle(color: currentColor),),),
                ),
              ),
              SizedBox(width: 6.0),
              Visibility(
                visible: children.length > 0,
                child: Center(
                  child: GestureDetector(
                    onTap: () {
                      setState(() {
                        widget.data.expaned = !widget.data.expaned;
                        if (widget.data.expaned) {
                          _rotationController.forward();
                        } else {
                          _rotationController.reverse();
                        }
                        if (widget.trailingOnTap != null &&
                            widget.trailingOnTap is Function) {
                          widget.trailingOnTap(widget.data);
                        }
                      });
                    },
                    child: RotationTransition(
                      child:const IconButton(
                            icon: Icon(Icons.expand_more),
                            iconSize: 16,
                            onPressed: null,
                          ),
                      turns: _turnsTween.animate(_rotationController),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
        Visibility(
          visible: children.length > 0 && widget.data.expaned,
          child: Padding(
            padding: EdgeInsets.only(left: level + 1 * offsetLeft),
            child: Column(
              children:  widget.data.children.map((item){
                return TreeNode(item,
                  titleOnTap: widget.titleOnTap,
                  leadingOnTap: widget.leadingOnTap,
                  trailingOnTap: widget.trailingOnTap,);
              }).toList()
            )))]);
  }
}
